package test.generic;

class Building {
}

class House extends Building {
}

/** 解决泛型擦除的问题--class法:
 *      在使用泛型的时候顺便将class传进去(调用构造方法的时候)
 * @param <T>
 */
public class GenericTestRTTI<T> {
    Class<T> kind;
    T t;

    public GenericTestRTTI(Class<T> kind) {
        this.kind = kind;
    }

    public boolean f(Object arg) {
        return kind.isInstance(arg);
    }

    public void newT() {
        try {
            t = kind.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        GenericTestRTTI<Building> ctt1 = new GenericTestRTTI<Building>(Building.class); // 这一步是关键, 将类的信息直接传进去
        System.out.println(ctt1.f(new Building()));
        System.out.println(ctt1.f(new House()));
        GenericTestRTTI<House> ctt2 = new GenericTestRTTI<House>(House.class);
        System.out.println(ctt2.f(new Building()));
        System.out.println(ctt2.f(new House()));
    }
}
